<template>
  <div class="app-container">
    <el-form class="searchForm" :inline="true" :model="queryForm" label-width="80px">
      <el-form-item label="key值">
        <el-input v-model="queryForm.keyinfo"> </el-input>
      </el-form-item>
      <el-form-item label="value值">
        <el-input v-model="queryForm.valueinfo"> </el-input>
      </el-form-item>
      <el-form-item label="分组">
        <el-select v-model="queryForm.groupinfo" placeholder="请选择" clearable>
          <el-option
            v-for="item in remarkData"
            :key="item.fvalue1"
            :label="item.fvalue2"
            :value="item.fvalue1"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="info" @click="pageChange(1)">查询</el-button>
        <!-- <el-button type="info" @click="getApprove">历史记录 </el-button> -->
        <el-button type="info" @click="onAdd">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="tableData"
      element-loading-background="rgba(0, 0, 0, 0.8)"
      height="550"
      stripe
      border
      style="width: 100%"
    >
      <el-table-column :resizable="false" label="操作" width="80" fixed>
        <template slot-scope="scope">
          <span class="edit-tooltip">
            <el-tooltip effect="dark" content="删除" placement="bottom">
              <el-button type="text" @click="onDel(scope.row)">
                <svg-icon style="fill: #ff4e1d" icon-class="butongyi"></svg-icon>
              </el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="修改" placement="bottom">
              <el-button type="text" @click="onClickName(scope.row)">
                <svg-icon style="fill: #ff4e1d" icon-class="a-xiugai"></svg-icon>
              </el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="审批记录" placement="bottom"> </el-tooltip>
          </span>
        </template>
      </el-table-column>
      <el-table-column :resizable="false" prop="id" label="唯一标识" min-width="120" />
      <el-table-column :resizable="false" prop="keyinfo" label="key值" min-width="100" />
      <el-table-column :resizable="false" prop="valueinfo" label="value值" min-width="120" />
      <el-table-column :resizable="false" prop="orderno" label="序号" min-width="120" />
      <el-table-column :resizable="false" prop="remark" label="分组名称" min-width="100" />
      <el-table-column :resizable="false" prop="groupinfo" label="分组编码" min-width="100" />
      <el-table-column :resizable="false" prop="parentid" label="父级标识" min-width="100" />
      <el-table-column :resizable="false" prop="tag" label="标识" min-width="100" />
      <el-table-column :resizable="false" prop="note" label="说明" min-width="120" />
    </el-table>
    <el-pagination
      style="margin: 10px"
      :current-page="queryForm.pagenum"
      :page-sizes="[20, 50, 100]"
      :page-size="queryForm.pagesize"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="sizeChange"
      @current-change="pageChange"
    />
    <!-- <Approve :visible="visible2" :documentid="approveid" :modelCode="approvetype" @dClose="dClose"></Approve> -->
    <el-dialog
      destroy-on-close
      :title="title"
      :visible.sync="visible"
      :close-on-click-modal="false"
      width="600px"
      @close="onClose"
    >
      <span>
        <el-form ref="form" :model="form" :rules="formRules" label-width="100px" :inline="false">
          <el-form-item label="key值" prop="keyinfo">
            <el-input v-model="form.keyinfo" />
          </el-form-item>
          <el-form-item label="value值" prop="valueinfo">
            <el-input v-model="form.valueinfo" />
          </el-form-item>
          <el-form-item label="分组名称" prop="remark">
            <el-input v-model="form.remark" />
            <!-- <el-select v-model="form.remark" placeholder="请选择" @change="remark(form)">
              <el-option
                v-for="(item, index) in remarkData"
                :key="'hhh' + index"
                :label="item.fvalue2"
                :value="item.fvalue2"
              ></el-option>
            </el-select> -->
          </el-form-item>
          <el-form-item label="分组编码" prop="groupinfo">
            <el-input v-model="form.groupinfo" />
          </el-form-item>

          <el-form-item label="序号" prop="orderno">
            <el-input v-model.number="form.orderno" />
          </el-form-item>
          <el-form-item label="父级标识" prop="parentid">
            <el-input v-model.number="form.parentid" />
          </el-form-item>
          <el-form-item label="标识" prop="tag">
            <el-input v-model="form.tag" />
          </el-form-item>
          <!-- <el-form-item label="备注" prop="remark">
            <el-input
              type="textarea"
              placeholder="请输入内容"
              v-model="form.remark"
              :autosize="{ minRows: 1, maxRows: 4 }"
              resize="none"
              show-word-limit
            />
          </el-form-item> -->
          <el-form-item label="说明" prop="note">
            <el-input
              v-model="form.note"
              type="textarea"
              placeholder="请输入内容"
              :autosize="{ minRows: 1, maxRows: 4 }"
              resize="none"
              show-word-limit
            />
          </el-form-item>
        </el-form>
      </span>
      <div slot="footer">
        <el-button type="info" @click="onSubmit">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { QueryPageList, GroupInfo, Save, Del } from '@/api/settings/zidian'
export default {
  name: 'Zidianweihu',
  data() {
    return {
      queryForm: {
        pagenum: 1,
        pagesize: 20
      },
      total: 0,
      tableData: [],
      remarkData: [],
      loading: true,
      visible: false,
      title: '字典维护',
      visible2: false,
      formRules: {
        keyinfo: [{ required: true, message: '请输入key值', trigger: 'blur' }],
        valueinfo: [{ required: true, message: '请输入value值', trigger: 'blur' }],
        orderno: [{ type: 'number', message: '序号必须为数字值' }],
        parentid: [{ type: 'number', message: '父级标识必须为数字值' }]
      },
      form: {},
      data2: []
    }
  },
  created() {
    this.onSearch()
  },
  methods: {
    // 点击页面显示条数
    sizeChange(size) {
      this.queryForm.pagesize = size
      this.onSearch()
    },
    // 点击下一页与页码
    pageChange(page) {
      console.log(page)
      this.queryForm.pagenum = page
      this.onSearch()
    },
    // 查询
    async onSearch() {
      this.loading = true
      const { content } = await QueryPageList(this.queryForm)
      const list = await GroupInfo()
      var res = []
      list.content.forEach(item => {
        if (item != null) {
          res.push(item)
        }
      })
      this.remarkData = res
      console.log(this.remarkData)
      this.tableData = content.list
      this.total = content.total
      this.loading = false
    },
    // 添加
    async onAdd() {
      console.log(new Date())
      // var time = this.$parseTime(new Date(), '{y}-{m}-{d}')
      const list = await GroupInfo()
      var res = []
      list.content.forEach(item => {
        if (item != null) {
          res.push(item)
        }
      })
      this.remarkData = res
      // this.form.time = time
      this.visible = true
    },
    onSubmit() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          await Save(this.form)
          if (this.form.id) {
            this.$message({
              type: 'success',
              message: '修改数据成功'
            })
          } else {
            this.$message({
              type: 'success',
              message: '添加数据成功'
            })
          }
          this.visible = false
          this.onSearch()
        } else {
          return false
        }
      })
    },
    onClickName(data) {
      this.visible = true
      this.form = data
    },
    onClose() {
      this.form = {}
    },
    onDel(data) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const del = await Del({ id: data.id })
          console.log(del)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.onSearch()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    remark(row) {
      console.log(row)

      this.remarkData.forEach(item => {
        if (item.fvalue2 == row.remark) {
          row.groupinfo = item.fvalue1
        }
      })
    }
    // getApprove() {
    //   this.visible2 = true
    //   this.data2 = []
    // },
    // dClose() {
    //   this.visible2 = false
    //   this.data2 = []
    // }
  }
}
</script>

<style lang="scss" scoped>
.title {
  height: 30px;
  line-height: 30px;
  background: rgba(0, 0, 0, 0.3);
  font-size: 14px;
  text-transform: uppercase;
  margin: 0 0 5px;
  padding: 0 10px;
  display: inline-block;
  letter-spacing: 0.5px;
}
</style>
